***********************************************************************************
********"Macroprudential Regulation, Quantitative Easing, and Bank Lending"********
***********************************************************************************
* This file sets up 3 datasets  that will be 
* used for the placebo test discussed in Appendix II. In particular:
* - a dataset with banks' securities
* - some pre-QE announcement variables
* - the dynamic of borrowing from the ECB
***********************************************************************************
***********************************************************************************


*==================================================================================
**# Bookmark (input)
clear *
import sas ENTE_SEGN CENSITO_CRITTOG TIME DISBUR COM COMUNE_DIP using $inputdirectory/bloans, case(lower)
run RO_creditregister tm(2013m12) tm(2013m12) 1

collapse (sum) disbur, by(bankid time)
rename disbur exbdisbur
recode exbdisbur (.=0)
xtset bankid time

* No assets, not in the sample
merge 1:1 bankid time using $inputdirectory/INB_assetsi, keepus(assets)
keep if _merge==3
drop _merge
rename assets exassets

* No bank type, not in the sample
merge 1:1 bankid using $inputdirectory/INB_typePLACEBO, keepus(type)
keep if _merge==3
drop _merge

merge 1:1 bankid using $inputdirectory/INB_gruPLACEBO, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
gen xxx=type if bankid==gru
egen grutype=max(xxx), by(gru)
replace grutype=10 if grutype==.
drop xxx

* No postal service and keep only banks with a joint-stock holding company
* LINE NOT SHOWN
keep if grutype==10

merge 1:1 bankid using $inputdirectory/INB_fsPLACEBO, keepus(cash)
keep if _merge!=2
drop _merge
rename cash excash
recode excash (.=0)

merge m:1 gru time using $inputdirectory/INB_ecbdepoPLACEBO, keepus(ecbdepo)
keep if _merge!=2
drop _merge
rename ecbdepo execbdepo
recode execbdepo  (.=0)

**# Bookmark (temporary dataset)
compress
save temp, replace

**# Bookmark (input)
clear all
import sas using $inputdirectory/isinsplacebo

rename *, lower
rename dat time
rename date_mat time_mat
rename ente_segn bankid
rename fonte source
egen isin=group(cod_isin)
tostring(time), replace
gen xxx=date(time, "YMD")
rename time oldtime
gen time=mofd(xxx)
drop xxx
format time %tm
tostring(time_mat), replace
gen xxx=date(time_mat, "YMD")
rename time_mat oldtime_mat
gen time_mat=mofd(xxx)
drop xxx
format time_mat %tm
replace time_mat=tm(2200m12) if time_mat==tm(9999m12)
gen monthtom=time_mat-time
drop if monthtom<0

* Group securities according to their accounting treatment
replace book=-book if portaf_contab==508|portaf_contab==509
gen portfolio=.
replace portfolio=1 if inlist(portaf_contab,504,505,506,512)
replace portfolio=2 if inlist(portaf_contab,503)
replace portfolio=3 if inlist(portaf_contab,501,502,0,508)
drop if portfolio==.

fillin bankid time portfolio
replace isin=0 if isin==.
replace book=0 if book==.

egen id=group(isin bankid portfolio)
collapse (sum) book (first) cod_isin isin bankid portfolio (max) staemitit setemi stanziabilita time_mat monthtom _fillin, by(id time)
xtset id time

* Banks in the sample
merge m:1 bankid using temp
keep if _merge==3
drop _merge
erase temp.dta

merge m:1 bankid using $inputdirectory/INB_gruPLACEBO, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop id
drop bankid
rename gru bankid

egen id=group(isin bankid portfolio)

collapse (sum) book exassets excash (first) cod_isin isin bankid portfolio execbdepo (max) staemitit setemi stanziabilita time_mat monthtom (min) _fillin , by(id time)

keep if time==tm(2013m12)

gen die=0
replace die=1 if inlist(staemitit,8,9,101,94,257,67,28,29,40,86,259,92,258,105,50,55,260,276,477,479,480,574,576,577,580,587,588,591,595,799,807,812,814,816,853,910,911,913,916,921,929,940,945,950,959,974)&inlist(setemi,100,704,770)&inlist(stanziabilita,23,24)&(monthtom>=27)&(monthtom<=375)&(monthtom!=.)

gen book2=book
replace book=0 if die!=1

keep if portfolio==3

collapse (sum) book book2 (max) exassets excash execbdepo, by(bankid)

gen e13=100*(book/exassets)
egen xxx = pctile(e13) if e13!=., p(85)
gen dtop85e13=(e13>xxx) if e13!=.
drop xxx
gen exlnassets=ln(exassets)
gen excashas=100*(excash+execbdepo)/(exassets)

**# Bookmark (saving dataset - file with different pre-announcement variables)
compress
save $inputdirectory/INB_preannPLACEBO, replace


*===============================================================================
**# Bookmark (input)
clear *
import sas using $inputdirectory/refin, case(lower)

rename dat date
tostring(date), replace
gen xxx=date(date, "YMD")
drop date
gen time=mofd(xxx)
drop xxx
format time %tm
rename rifinbil refin
rename ente_segn bankid
drop abi

*GROUP SETUP
merge m:1 bankid using $inputdirectory/INB_gruPLACEBO, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop bankid
rename gru bankid

collapse (sum) refin, by(bankid time)

merge m:1 bankid using $inputdirectory/INB_preannPLACEBO, keepus(exassets)
drop exassets
keep if _merge==3|_merge==2
drop _merge
xtset bankid time
replace time=tm(2013m12) if time==.
tsfill, full
recode refin (.=0)
gen lncrefin=100*(ln(refin+1)-ln(L.refin+1))

**# Bookmark (saving dataset - dynamic of borrowing from the ECB)
compress
save $inputdirectory/INB_refinmPLACEBO, replace
